function hfile2 = dtsf_SaveAsFBR(hfile, fbrfilename, bogus)
% DTSF::SaveAsFBR  - convert a DTI Studio FiberDat into FBR file
%
% FORMAT:       [fbr] = dtsf.SaveAsFBR(fbrfilename [, bogus]);
%
% Input fields:
%
%       fbrfilename FBR filename
%       bogus       if given and true, add bogus group
%
% Output fields:
%
%       fbr         FBR object

% Version:  v0.7b
% Build:    7083009
% Date:     Aug-30 2007, 9:05 AM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'dtsf') || ...
   ~ischar(fbrfilename) || ...
    isempty(fbrfilename)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end

% create empty FBR file in memory
hfile2 = BVQXfile('new:fbr');

% check file writability
try
    aft_SaveAs(hfile2, fbrfilename);
    nbc = bvqxfile_getcont(hfile2.L);
catch
    BVQXfile(0, 'clearobj', hfile2.L);
    error( ...
        'BVQXfile:FileNotWritable', ...
        'File not writable: ''%s''.', ...
        fbrfilename ...
    );
end

% get source space
bc = bvqxfile_getcont(hfile.L);
pix1 = bc.DimX;
pix2 = bc.DimY;
pix3 = bc.DimZ;
res1 = bc.ResX;
res2 = bc.ResY;
res3 = bc.ResZ;
sori = bc.SliceOrientation;
if bc.SliceSequencing
    res3 = -res3;
end
cnt1 = (res1 * pix1) / 2;
cnt2 = (res2 * pix2) / 2;
cnt3 = (res3 * pix3) / 2;

% create coordinate conversion matrix, slice orientation
switch (sori)

    % coronal slicing (along BV's X axis)
    case {0}
        tmat = [ ...
               0,    0, res3, -cnt3 ; ...
               0, res2,    0, -cnt2 ; ...
            res1,    0,    0, -cnt1 ; ...
               0,    0,    0,     1];
        
    % axial slicing (along BV's Y axis)
    case {1}
        tmat = [ ...
               0, res2,    0, -cnt2 ; ...
               0,    0, res3, -cnt3 ; ...
            res1,    0,    0, -cnt1 ; ...
               0,    0,    0,     1];

    % sagittal slicing (along BV's Z axis)
    case {2}
        tmat = [ ...
            res1,    0,    0, -cnt1 ; ...
               0, res2,    0, -cnt2 ; ...
               0,    0, res3, -cnt3 ; ...
               0,    0,    0,     1];

    otherwise
        BVQXfile(0, 'clearobj', hfile2.L);
        error( ...
            'BVQXfile:InvalidField', ...
            'Invalid SliceOrientation field content.' ...
        );
end

% create new fiber struct
nfb.NrOfPoints = 1;
nfb.FiberPoints = [0 0 0];

% put new fibers into FBR
numf = bc.NrOfFibers;
nbc.Group(1).NrOfFibers = numf;
nbc.Group(1).Fiber = nfb(ones(numf, 1));

% add bogus group ?
if nargin > 2 && ...
   (islogical(bogus) || isnumeric(bogus)) && ...
   ~isempty(bogus) && ...
    bogus(1)
    nbc.NrOfGroups = 2;
    nbc.Group(2).Name = 'BogusGroup';
    nbc.Group(2).Visible = 1;
    nbc.Group(2).Animate = 0;
    nbc.Group(2).Thickness = 0.3;
    nbc.Group(2).Color = [0 0 0];
    nbc.Group(2).NrOfFibers = 1;
    nbc.Group(2).Fiber = nfb;
    nbc.Group(2).Fiber.FiberPoints = [-128 -128 -128];
end

% loop over fibers in FiberDat
for fc = 1:numf
    fiber = bc.Fibers(fc);
    fcoor = fiber.Coord;
    nfb.NrOfPoints = size(fcoor, 1);
    fcoor(:, 4) = 1;
    fcoort = (tmat * fcoor')';
    nfb.FiberPoints = fcoort(:, 1:3) + 128;
    nbc.Group(1).Fiber(fc) = nfb;
end

% put back into content
bvqxfile_setcont(hfile.L, nbc);

% save to file
aft_SaveAs(hfile2, fbrfilename);
